*************************************************************************************************
*																								*
*						Flexible Wages, Bargaining, and The Gender Gap							*
*						Barbara Biasi and Heather Sarsons										*
*						Figures and Appendix Figures using CPS data								*
*																								*
*************************************************************************************************


clear all
set more off
set matsize 11000
set maxvar 32000


global user = 2 // 1 = Heather, 2 = Barbara

if $user == 1 {
cd "/Users/sarsons/Dropbox/wisconsin_women/data"
global tab = "/Users/sarsons/Dropbox/wisconsin_women/draft/tables"
global out = "/Users/sarsons/Dropbox/wisconsin_women/draft/tex/QJE_resubmission/QJE_files"
global dp = "/Users/sarsons/Dropbox/wisconsin_women/do"
}
if $user == 2 {
cd "~/Dropbox/Research/wisconsin_women/data"
global tab = "~/Dropbox/Research/wisconsin_women/draft/tables"
global out = "~/Dropbox/Research/wisconsin_women/draft/tex/QJE_resubmission/QJE_files"
global RA = "~/Dropbox/Research/wisconsin_women/do/calvin"
global do = "~/Dropbox/Research/wisconsin_women/do"
}


*use CPS/ipums_cps_2010_2019_clean.dta, clear
use CPS_210306_BB/CPS_raw_BB.dta, clear
gen teacher = occ2010 > 2200 & occ2010 < 2340 
gen employed = empstat == 10
gen public = classwkr == 27 | classwkr == 28
gen ba = educ == 111
gen ma = educ >= 123
gen fulltime = wkstat == 11
drop if year == 2021 | union == 0

* age groups
gen agegr = 3*round(age/3)
replace agegr = 18 if agegr < 18
replace agegr = 75 if agegr > 75

* weekly earnings
replace earnweek = . if earnweek == 9999.99

gen loginc = ln(earnweek)
capt gen female = sex == 2

global cond = "employed == 1 & fulltime == 1 & teach == 1 & public == 1"

* Basic Facts: gender gap and union coverage, public school teachers
gen gendergap = .
gen membership = .
gen Union = union >= 2
forvalues y = 2000/2019 {
	local z = `y' - 2
	local x = `y' + 2
	reghdfe loginc female if year >= `z' & year <= `x' & $cond  [aw = wtfinl], a(agegr#ba#ma occ2010 state#year) vce(robust)
	replace gendergap = _b[female] if year == `y'
	qui sum Union if year == `y' & $cond
	replace membership = `r(mean)' if year == `y'
}
preserve
collapse gendergap membership, by(year)
twoway (connected gender year, color(black) lp(dash)) (connected membership year, yaxis(2) color(gs8) lp(solid)) ///
		if year >= 2005, legend(order(1 "gender gap" 2 "union membership") pos(6) row(1)) ///
		ylabel(-0.14(0.02)-0.08) ylabel(0.55(0.05)0.80, axis(2)) ///
		ytitle("gender gap, log weekly earnings: women vs men") ///
		ytitle("union membership or coverage", axis(2))
graph export "$out/CPS_gap_membership_teachers.tif", replace height(600)
restore	


* Gender gap is larger in states-industries where union membership declined
gen ind = floor(ind1990/100)
gen occ = floor(occ2010/1000)
gen privpub = classwkr
recode privpub (22 = 1) (23 = 1) (25 = 2) (27 = 2) (27 = 2)
bysort ind occ statef privpub: egen U05 = mean(Union) if year >= 2005 & year < 2010
bysort ind occ statef privpub: egen U20 = mean(Union) if year >= 2015 & year <= 2019
tempvar X
tempvar Y
bysort ind occ statef privpub: egen `X' = max(U05)
bysort ind occ statef privpub: egen `Y' = max(U20)
gen Uchange = (`Y' - `X')
replace Uchange = . if `X' == . | `Y' == .

preserve
	gen gapdrop = .
	gen undrop = .
	gen gapall = .
	gen unall = .
	gen gaprise = .
	gen unrise = .
forvalues y = 2000/2019 {
	global Cond = "employed == 1 & fulltime == 1"
	global Conddrop = "employed == 1 & fulltime == 1 & Uchange < -.02 & Uchange != ."
	global Condrise = "employed == 1 & fulltime == 1 & Uchange > .05 & Uchange != ."
	local z = `y' - 2
	local x = `y' + 2
	reghdfe loginc female if year >= `z' & year <= `x' & $Cond  [aw = wtfinl], a(agegr#ba#ma occ#ind#privpub#state#year) vce(robust)
	replace gapall = _b[female] if year == `y'
	qui sum Union if year == `y' & $Cond
	replace unall = `r(mean)' if year == `y'
	reghdfe loginc female if year >= `z' & year <= `x' & $Conddrop  [aw = wtfinl], a(agegr#ba#ma occ#ind#privpub#state#year) vce(robust)
	replace gapdrop = _b[female] if year == `y'
	qui sum Union if year == `y' & $Conddrop
	replace undrop = `r(mean)' if year == `y'
	
	reghdfe loginc female if year >= `z' & year <= `x' & $Condrise  [aw = wtfinl], a(agegr#ba#ma occ#ind#privpub#state#year) vce(robust)
	replace gaprise = _b[female] if year == `y'
	qui sum Union if year == `y' & $Condrise
	replace unrise = `r(mean)' if year == `y'
}
collapse gapdrop undrop gaprise unrise, by(year)
twoway 	(connected gapdrop year, color(black) lp(dash)) (connected undrop year, yaxis(2) color(gs8) lp(solid)) ///
		if year >= 2005, legend(order(1 "gender gap" 2 "union membership") pos(6) row(1)) ///
		ylabel(-0.24(0.02)-0.16) ylabel(0.10(0.20)0.50, axis(2)) ///
		ytitle("gender gap, log weekly earnings: women vs men") ///
		ytitle("union membership or coverage", axis(2) color(gs8))
graph export "$out/CPS_gap_membership_drop.tif", replace
twoway 	(connected gaprise year, color(black) lp(dash)) (connected unrise year, yaxis(2) color(gs8) lp(solid)) ///
		if year >= 2005, legend(order(1 "gender gap" 2 "union membership") pos(6) row(1)) ///
		ylabel(-0.24(0.02)-0.16) ylabel(0.10(0.20)0.50, axis(2)) ///
		ytitle("gender gap, log weekly earnings: women vs men") ///
		ytitle("union membership or coverage", axis(2) color(gs8))
graph export "$out/CPS_gap_membership_rise.tif", replace height(600)
restore





